package com.food.eat.impl;

import com.food.eat.enums.CommentsInfo;
import com.food.eat.mapper.*;
import com.food.eat.pojo.*;
import com.food.eat.pojo.vo.CommentLevelCountVo;
import com.food.eat.service.ItemsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:RenPu
 * @Date: 2020/7/21 14:47
 * @Version: 1.0
 * @description:
 */


@Service
public class ItemsServiceImpl implements ItemsService {


    @Resource
    private ItemsMapper itemsMapper;

    @Resource
    private ItemsImgMapper itemsImgMapper;

    @Resource
    private ItemsSpecMapper itemsSpecMapper;

    @Resource
    private ItemsParamMapper itemsParaMapper;

    @Resource
    private ItemsCommentsMapper itemsCommentsMapper;


    /**
     * 根据id值获取所对应的商品
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Items queryByOneItems(String itemId) {
        return itemsMapper.selectByPrimaryKey(itemId);
    }


    /**
     * 根据id值获取所对应的商品的多张图片
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsImg> queryManyItemsImg(String itemId) {

        Example exampleImg = new Example(ItemsImg.class);
        Example.Criteria criteria = exampleImg.createCriteria();
        criteria.andEqualTo("itemId", itemId);
        return itemsImgMapper.selectByExample(exampleImg);
    }


    /**
     * 根据id值获取所对应的商品多种商品规格：例如：不同的口味
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsSpec> queryManyItemsSpec(String itemId) {

        Example exampleSpec = new Example(ItemsSpec.class);
        Example.Criteria criteria = exampleSpec.createCriteria();
        criteria.andEqualTo("itemId", itemId);
        return itemsSpecMapper.selectByExample(exampleSpec);
    }


    /**
     * 根据id值获取所对应的商品的描述信息
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ItemsParam queryByOneItemsParam(String itemId) {

        Example exampleParam = new Example(ItemsParam.class);
        Example.Criteria criteria = exampleParam.createCriteria();
        criteria.andEqualTo("itemId", itemId);
        return itemsParaMapper.selectOneByExample(exampleParam);
    }


    /**
     * 查询评论的好，中，差，总的数量进行展示
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public CommentLevelCountVo queryCommentsCount(String itemId) {

        Integer goodCount = getCommentsCount(itemId, CommentsInfo.GOOD.type);
        Integer normalCount = getCommentsCount(itemId, CommentsInfo.NORMAL.type);
        Integer badCount = getCommentsCount(itemId, CommentsInfo.BAD.type);
        Integer totalCount = goodCount + normalCount + badCount;

        CommentLevelCountVo commentLevelCountVo = new CommentLevelCountVo();
        commentLevelCountVo.setBadCounts(badCount);
        commentLevelCountVo.setGoodCounts(goodCount);
        commentLevelCountVo.setNormalCounts(normalCount);
        commentLevelCountVo.setTotalCounts(totalCount);
        return commentLevelCountVo;
    }


    /**
     * 将计算的评价的数量，封装为一个方法
     *
     * @param itemId
     * @param level
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public Integer getCommentsCount(String itemId, Integer level) {

        ItemsComments itemsComments = new ItemsComments();
        itemsComments.setItemId(itemId);
        if (level != null) {
            itemsComments.setCommentLevel(level);
        }
        return itemsCommentsMapper.selectCount(itemsComments);
    }


}
